package sequential_apriori;

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class DataReader {

	// Object variables
	private String dataURL;

	public DataReader(String dataURL) {
		this.dataURL = dataURL;
	}

	/* Read in at most numLines number of lines from the text file at dataURL and return as a single string */
	public String loadDataLines(int numLines, Boolean stripLines) {
		
		// Initialize return string
		String dataLines = "";
		
		// Read in from file using a scanner and check for IO errors
		int i = 0;
		try {
			// Read in UTF-8 encoding
			Scanner scanner = new Scanner(new FileInputStream(this.dataURL), "UTF-8");
			
			// Iterate until the max number of lines is reached, or there are no lines left
			while (i<numLines && scanner.hasNextLine()) {
				
				// Get the next line
				String line = scanner.nextLine();
				
				// If stripLines is set to true, only include lines starting with the 'Q' identifier
				if(!stripLines || line.charAt(0)=='Q') {
					// Strip the identifier character from each string
					dataLines += line.substring(1) + "\n";
				}
				i++;
			}
			scanner.close();
		} catch(FileNotFoundException e) {
			// Check that the file exists
			System.out.println("Error: File not found!");
		}

		// Return the data string
		return dataLines;
	}

}
